import { ref } from 'vue'
import type { FormInstance } from 'element-plus'
import { ElMessage } from 'element-plus'

export interface Todo {
  id: string
  title: string
  date: Date | null
  time: Date | null
  description: string
  color: string
  completed: boolean
  reminder: boolean
}

export function useTodos() {
  // 添加待办对话框显示状态
  const showAddTodoDialog = ref(false)
  
  // 表单引用
  const todoFormRef = ref<FormInstance>()
  
  // 表单数据
  const todoForm = ref({
    title: '',
    date: null,
    time: null,
    description: '',
    color: '#409EFF',
    reminder: false
  })

  // 表单验证规则
  const todoRules = {
    title: [
      { required: true, message: '请输入待办事项标题', trigger: 'blur' },
      { min: 2, max: 50, message: '长度在 2 到 50 个字符', trigger: 'blur' }
    ],
    date: [
      { required: true, message: '请选择日期', trigger: 'change' }
    ],
    time: [
      { required: true, message: '请选择时间', trigger: 'change' }
    ]
  }

  // 重置表单
  const resetForm = () => {
    if (todoFormRef.value) {
      todoFormRef.value.resetFields()
    }
    todoForm.value = {
      title: '',
      date: null,
      time: null,
      description: '',
      color: '#409EFF',
      reminder: false
    }
  }

  // 添加待办事项
  const handleAddTodo = async () => {
    if (!todoFormRef.value) return
    
    await todoFormRef.value.validate((valid) => {
      if (valid) {
        // TODO: 添加待办事项到存储
        showAddTodoDialog.value = false
        ElMessage.success('添加成功')
        resetForm()
      }
    })
  }

  // 获取指定日期的待办事项
  const getTodosByDate = (date: Date) => {
    // TODO: 从存储中获取待办事项
    return []
  }

  return {
    showAddTodoDialog,
    todoFormRef,
    todoForm,
    todoRules,
    handleAddTodo,
    getTodosByDate,
    resetForm
  }
} 